package com.shababeach.travel.controller;

import com.shababeach.travel.dto.Result;
import com.shababeach.travel.service.UserService;
import com.shababeach.travel.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取用户信息
     *
     * @param token 认证token
     * @return 用户信息
     */
    @GetMapping("/info")
    public Result<Map<String, Object>> getUserInfo(@RequestHeader("Authorization") String token) {
        try {
            // 从token中获取用户ID
            String userId = jwtUtil.getUserIdFromToken(token);
            
            // 获取用户信息
            Map<String, Object> userInfo = userService.getUserInfo(Long.parseLong(userId));
            
            return Result.success(userInfo);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
} 